package com.haoran.dwz.common.scheduler;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 调度中心
 */
public class SchedulerCenter {

	public static void main(String[] args) throws SchedulerException, InterruptedException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("Current Time Is : " + sf.format(new Date()));
		JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).usingJobData("msg", "sssss")
				.withIdentity("myJob", "job-group-1").build();

		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "trigger-group-1")
				.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?")).build();

		// 创建Scheduler实例
		SchedulerFactory sfact = new StdSchedulerFactory();
		Scheduler scheduler = sfact.getScheduler();
		scheduler.start();
		System.out.println("scheduled time is :" + sf.format(scheduler.scheduleJob(jobDetail, trigger)));
		// scheduler执行两秒后挂起
		// Thread.sleep(2000L);
		// shutdown(true)表示等待所有正在执行的job执行完毕之后，再关闭scheduler
		// shutdown(false)即shutdown()表示直接关闭scheduler
		// scheduler.shutdown(false);
		// System.out.println("scheduler is shut down? " + scheduler.isShutdown());
	}

}
